import { promisic } from './utils.js'
import { Throwable } from "../err_handle/throwable.js"
import { authorize } from "./authorize.js"

export const getLocation = function({ isHighAccuracy = false, showLoading = false } = {}) {
	return authorize({
			scope: "scope.userLocation",
			desc: "无法获取您的位置，您需要在设置中开启\"位置信息\"选项。"
		})
		.then(() => {
			if(showLoading) {
				uni.showLoading({
					mask: true
				})
			}
			if (isHighAccuracy) {
				return promisic(uni["getLocation"])({ isHighAccuracy: isHighAccuracy, highAccuracyExpireTime: 4000 , type: 'gcj02'})
					.catch((error) => {
						console.log("getLocation", error)
						return Promise.reject(new Throwable(10000, "getLocation:fail:系统错误"))
					})
			} else {
				return promisic(uni["getLocation"])()
					.catch((error) => {
						return Promise.reject(new Throwable(10000, "getLocation:fail:系统错误"))
					})
			}
		})
		.then((res) => {
			return Promise.resolve({
				latitude: res.latitude,
				longitude: res.longitude
			})
		})
		.finally(() => {
			if(showLoading) {
				uni.hideLoading()
			}
		})
}
